package cn.lijiajia3515.cairo.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.List;


/**
 * 跨域配置
 */
@Configuration(proxyBeanMethods = false)
public class CorsConfig {

	@Bean
	CorsConfigurationSource corsConfigurationSource() {
		CorsConfiguration cors = new CorsConfiguration();
		cors.setAllowedOriginPatterns(List.of("http*"));
		// 允许所有公开请求头 跨域
		cors.setExposedHeaders(List.of("*"));
		// 允许 所有请求头跨域
		cors.setAllowedHeaders(List.of("*"));
		// 允许 所有方法跨域
		cors.setAllowedMethods(List.of("*"));
		// 允许携带凭证
		cors.setAllowCredentials(true);
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/oauth2/**", cors);
		source.registerCorsConfiguration("/api/**", cors);
		source.registerCorsConfiguration("/.well-known/**", cors);
		return source;
	}
}
